/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ModificaUsuarioView.java
 *
 * Created on 15-ene-2011, 0:00:59
 */
package Vista.Usuario;

import Modelo.CompruebaDNI;
import Modelo.Entidades.Usuario;
import javax.swing.JOptionPane;

/*
 *  Proyecto eJAM [G12]
 *  Miembros:
 *      - Javier Ruiz Hidalgo
 *      - Adrian Ramos Cápitas
 *      - Miguel Alejandro Esteban Ordóñez
 */

public class ModificaUsuarioView extends javax.swing.JPanel {

    private UsuarioViewImpl view;
    private Usuario usuarioProvisional = null;

    /** Creates new form ModificaUsuarioView */
    public ModificaUsuarioView(UsuarioViewImpl view) {
        initComponents();
        this.view = view;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComprobacionDNI = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jDNI = new javax.swing.JTextField();
        jSemaforo = new javax.swing.JPanel();
        jComprobar = new javax.swing.JButton();
        jDatos = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jApellido1 = new javax.swing.JTextField();
        jApellido2 = new javax.swing.JTextField();
        jNombre = new javax.swing.JTextField();
        jTelefono = new javax.swing.JTextField();
        jCorreo = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jBorrar = new javax.swing.JButton();
        jAceptar = new javax.swing.JButton();

        jComprobacionDNI.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Comprobación DNI", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jLabel1.setText("DNI: ");

        jSemaforo.setBackground(java.awt.Color.yellow);
        jSemaforo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        javax.swing.GroupLayout jSemaforoLayout = new javax.swing.GroupLayout(jSemaforo);
        jSemaforo.setLayout(jSemaforoLayout);
        jSemaforoLayout.setHorizontalGroup(
            jSemaforoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 18, Short.MAX_VALUE)
        );
        jSemaforoLayout.setVerticalGroup(
            jSemaforoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 16, Short.MAX_VALUE)
        );

        jComprobar.setText("Comprobar");
        jComprobar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComprobarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jComprobacionDNILayout = new javax.swing.GroupLayout(jComprobacionDNI);
        jComprobacionDNI.setLayout(jComprobacionDNILayout);
        jComprobacionDNILayout.setHorizontalGroup(
            jComprobacionDNILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jComprobacionDNILayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDNI, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSemaforo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jComprobar, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );
        jComprobacionDNILayout.setVerticalGroup(
            jComprobacionDNILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jComprobacionDNILayout.createSequentialGroup()
                .addGroup(jComprobacionDNILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jComprobacionDNILayout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jComprobar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jComprobacionDNILayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(jComprobacionDNILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jSemaforo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jDNI, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1))))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        jDatos.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jLabel2.setText("* Nombre: ");

        jLabel3.setText("* 1º Apellido: ");

        jLabel4.setText("2º Apellido:");

        jLabel5.setText("* Telefono");

        jLabel6.setText("E-mail: ");

        jApellido1.setEnabled(false);

        jApellido2.setEnabled(false);

        jNombre.setEnabled(false);

        jTelefono.setEnabled(false);

        jCorreo.setEnabled(false);

        javax.swing.GroupLayout jDatosLayout = new javax.swing.GroupLayout(jDatos);
        jDatos.setLayout(jDatosLayout);
        jDatosLayout.setHorizontalGroup(
            jDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDatosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCorreo, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                    .addComponent(jTelefono, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                    .addComponent(jApellido2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                    .addComponent(jApellido1, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                    .addComponent(jNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE))
                .addGap(35, 35, 35))
        );
        jDatosLayout.setVerticalGroup(
            jDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDatosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jApellido1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jApellido2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jCorreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jBorrar.setText("Borrar");
        jBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBorrarActionPerformed(evt);
            }
        });

        jAceptar.setText("Aceptar");
        jAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(116, 116, 116)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jDatos, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComprobacionDNI, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jAceptar)
                                .addGap(18, 18, 18)
                                .addComponent(jBorrar))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 670, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jComprobacionDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jDatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBorrar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jAceptar))
                .addContainerGap(179, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jComprobarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComprobarActionPerformed

        if (CompruebaDNI.getInstance().compruebaDNI(this.jDNI.getText()) == true) {
            Usuario usuario = this.view.getModel().obtenerEntidad(this.jDNI.getText());

            if (usuario != null) {
                jNombre.setText(usuario.getNombre());
                jApellido1.setText(usuario.getApellido1());
                jApellido2.setText(usuario.getApellido2());
                jTelefono.setText(usuario.getTelefono());
                jCorreo.setText(usuario.getCorreo());

                this.desbloquearCampos();
                jSemaforo.setBackground(java.awt.Color.GREEN);
                jDNI.disable();
                this.usuarioProvisional = usuario;
            } else {
                this.bloquearCampos();
                jSemaforo.setBackground(java.awt.Color.RED);
                JOptionPane.showMessageDialog(null, "Error: Usuario no Registrado");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Error: DNI Erroneo");
        }
}//GEN-LAST:event_jComprobarActionPerformed

    private void jBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBorrarActionPerformed
        this.removeAll();
        this.initComponents();
}//GEN-LAST:event_jBorrarActionPerformed

    private void jAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAceptarActionPerformed

        if (jNombre.getText().equals("") || jApellido1.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Error: Campos Imprescindibles Vacios");
        }
        else {
            if (this.usuarioProvisional.getClass().getName().equals("Modelo.Entidad.AlumnoImpl")) {
                this.view.fireModificarUsuarioGesture(jNombre.getText(), jApellido1.getText(), jApellido2.getText(), jDNI.getText(), jTelefono.getText(), jCorreo.getText(), "Alumno");
            }
            else {
                this.view.fireModificarUsuarioGesture(jNombre.getText(), jApellido1.getText(), jApellido2.getText(), jDNI.getText(), jTelefono.getText(), jCorreo.getText(), "Profesor");
            }
        }

        this.jBorrarActionPerformed(evt);
}//GEN-LAST:event_jAceptarActionPerformed

    private void desbloquearCampos() {
        this.jNombre.enable();
        this.jApellido1.enable();
        this.jApellido2.enable();
        this.jCorreo.enable();
        this.jTelefono.enable();
    }

    private void bloquearCampos() {
        this.jNombre.disable();
        this.jApellido1.disable();
        this.jApellido2.disable();
        this.jCorreo.disable();
        this.jTelefono.disable();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAceptar;
    private javax.swing.JTextField jApellido1;
    private javax.swing.JTextField jApellido2;
    private javax.swing.JButton jBorrar;
    private javax.swing.JPanel jComprobacionDNI;
    private javax.swing.JButton jComprobar;
    private javax.swing.JTextField jCorreo;
    private javax.swing.JTextField jDNI;
    private javax.swing.JPanel jDatos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jNombre;
    private javax.swing.JPanel jSemaforo;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTelefono;
    // End of variables declaration//GEN-END:variables
}
